home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / lib / Arraychar.p < prev    next >
Text File  |  1990-05-19  |  2KB  |  122 lines

  1. /* Arraychar.p -- type-specific functions for class Arraychar
  2.  
  3. Author:
  4.     K. E. Gorlen
  5.     Bg. 12A, Rm. 2033
  6.     Computer Systems Laboratory
  7.     Division of Computer Research and Technology
  8.     National Institutes of Health
  9.     Bethesda, Maryland 20892
  10.     Phone: (301) 496-1111
  11.     uucp: uunet!nih-csl!kgorlen
  12.     Internet: kgorlen@alw.nih.gov
  13.     September, 1985
  14.  
  15. Function:
  16.     
  17. Type-specific functions for class Arraychar, such as hash() and
  18. printOn().  Generic functions are generated from the m4 template file
  19. Array.c.m4.
  20.  
  21. WARNING -- Make changes to the .p or .m4 files, not to the .c file
  22. they generate.
  23.  
  24. $Log:    Arraychar.p,v $
  25.  * Revision 3.0  90/05/20  00:19:00  kgorlen
  26.  * Release for 1st edition.
  27.  * 
  28. */
  29.  
  30. `#include' "Arraychar.h"
  31. `#include' "nihclconfig.h"
  32. `#include' <iomanip.h>
  33. `#include' <libc.h>
  34. `#include' "nihclIO.h"
  35.  
  36. `#define'    THIS    Arraychar
  37. `#define'    BASE    Collection
  38. #define BASE_CLASSES BASE::desc()
  39. #define MEMBER_CLASSES
  40. #define VIRTUAL_BASE_CLASSES
  41.  
  42. DEFINE_CLASS(Arraychar,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/Arraychar.p,v 3.0 90/05/20 00:19:00 kgorlen Rel $",NULL,NULL);
  43.  
  44. static int charCmp(const void* a, const void* b)
  45. {
  46.     return *(const unsigned char*)a - *(const unsigned char*)b;
  47. }
  48.  
  49. union hash_char_mask {
  50.     unsigned in[sizeof(int)];
  51.     char ch[sizeof(int)*sizeof(int)];
  52.     hash_char_mask();
  53. } mask;
  54.  
  55. hash_char_mask::hash_char_mask()
  56. {
  57.     for (register unsigned i=0; i<sizeof(int); i++) {
  58.         for (register unsigned j=0; j<sizeof(int); j++) ch[sizeof(int)*i+j] = j<i ? 0xff : 0;
  59.     }
  60. }
  61.  
  62. unsigned THIS::hash() const
  63. {
  64.     register unsigned h = sz;
  65.     register unsigned i = div_sizeof_int(sz);
  66.     register unsigned* vv = (unsigned*)v;
  67.     while (i--) h ^= *vv++;
  68.     if ((i = mod_sizeof_int(sz)) != 0)
  69.         h ^= *vv & mask.in[i];
  70.     return h;
  71. }
  72.  
  73. void THIS::printOn(ostream& strm) const
  74. {
  75.     for (register unsigned i=0; i<sz; i++) {
  76.         if (i > 0) strm << "  ";
  77.          strm << "0x" << setfill('0') << setw(2)
  78.               << hex << ((unsigned int)(unsigned char)v[i]);
  79.     }
  80.     strm << dec;
  81. }
  82.  
  83. THIS::THIS(OIOin& strm)
  84. :
  85. #ifdef MI
  86.     Object(strm),
  87. #endif
  88.     BASE(strm)
  89. {
  90.     strm >> sz;
  91.     v = NEW(char,sz);
  92.     strm.get(v,sz);
  93. }
  94.  
  95. void THIS::storer(OIOout& strm) const
  96. {
  97.     BASE::storer(strm);
  98.     strm << sz;
  99.     strm.put(v,sz);
  100. }
  101.  
  102. Arraychar::Arraychar(OIOifd& fd)
  103. :
  104. #ifdef MI
  105.     Object(fd),
  106. #endif
  107.     BASE(fd)
  108. {
  109.     fd >> sz;
  110.     v = NEW(char,sz);
  111.     fd.get(v,sz);
  112. }
  113.  
  114. void Arraychar::storer(OIOofd& fd) const
  115. {
  116.     BASE::storer(fd);
  117.     fd << sz;
  118.     fd.put(v,sz);
  119. }
  120.  
  121. ARRAYIMPLEMENT(Arraychar,char)
  122.